package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.MchntFeeTxnAmtLimitService;
import cc.rengu.igas.momp.facade.request.EditMchntFeeTxnAmtLimitRequest;
import cc.rengu.igas.momp.facade.request.QueryMchntFeeTxnAmtLimitRequest;
import cc.rengu.igas.momp.facade.response.EditMchntFeeTxnAmtLimitResponse;
import cc.rengu.igas.momp.facade.response.QueryMchntFeeTxnAmtLimitResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;

/**
 * 商户当前周期手续费优惠限额接口实现
 *
 * @author liujinan
 * @version 1.0.0
 * @date 2021/6/21 09:52
 */
@SuppressWarnings("unchecked")
public class MchntFeeTxnAmtLimitServiceImpl implements MchntFeeTxnAmtLimitService {
    @Override
    public Result<QueryMchntFeeTxnAmtLimitResponse> queryMchntFeeTxnAmtLimit(QueryMchntFeeTxnAmtLimitRequest queryMchntFeeTxnAmtLimitRequest) {

        DubboService<QueryMchntFeeTxnAmtLimitRequest> dubboService = new DubboServiceImpl();
        Result<QueryMchntFeeTxnAmtLimitResponse> queryMchntFeeTxnAmtLimitResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_MCHNTFEETXNAMT_LIMIT.getTxnNum().equals(queryMchntFeeTxnAmtLimitRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryMchntFeeTxnAmtLimitResponse> dubboResult = dubboService.callAtomService(queryMchntFeeTxnAmtLimitRequest,
                    MompTxnNumEnum.QUERY_MCHNTFEETXNAMT_LIMIT.getService(), QueryMchntFeeTxnAmtLimitResponse.class);
            queryMchntFeeTxnAmtLimitResponseResult.setResult(dubboResult.getResult());
            queryMchntFeeTxnAmtLimitResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryMchntFeeTxnAmtLimitResponse manageAgentProfitSharingMcResponse = new QueryMchntFeeTxnAmtLimitResponse();
            queryMchntFeeTxnAmtLimitResponseResult.setResult(manageAgentProfitSharingMcResponse);
            manageAgentProfitSharingMcResponse.setHeader(queryMchntFeeTxnAmtLimitRequest.getHeader());
            queryMchntFeeTxnAmtLimitResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                manageAgentProfitSharingMcResponse.setRespCode(((BizException) e).getCode());
                manageAgentProfitSharingMcResponse.setRespDesc(e.getMessage());
            } else {
                manageAgentProfitSharingMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                manageAgentProfitSharingMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }
        return queryMchntFeeTxnAmtLimitResponseResult;
    }

    @Override
    public Result<EditMchntFeeTxnAmtLimitResponse> editMchntFeeTxnAmtLimit(EditMchntFeeTxnAmtLimitRequest editMchntFeeTxnAmtLimitRequest) {

        DubboService<EditMchntFeeTxnAmtLimitRequest> dubboService = new DubboServiceImpl();
        Result<EditMchntFeeTxnAmtLimitResponse> editMchntFeeTxnAmtLimitResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.EDIT_MCHNTFEETXNAMT_LIMIT.getTxnNum().equals(editMchntFeeTxnAmtLimitRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<EditMchntFeeTxnAmtLimitResponse> dubboResult = dubboService.callAtomService(editMchntFeeTxnAmtLimitRequest,
                    MompTxnNumEnum.EDIT_MCHNTFEETXNAMT_LIMIT.getService(), EditMchntFeeTxnAmtLimitResponse.class);
            editMchntFeeTxnAmtLimitResponseResult.setResult(dubboResult.getResult());
            editMchntFeeTxnAmtLimitResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            EditMchntFeeTxnAmtLimitResponse editMchntFeeTxnAmtLimitResponse = new EditMchntFeeTxnAmtLimitResponse();
            editMchntFeeTxnAmtLimitResponseResult.setResult(editMchntFeeTxnAmtLimitResponse);
            editMchntFeeTxnAmtLimitResponse.setHeader(editMchntFeeTxnAmtLimitRequest.getHeader());
            editMchntFeeTxnAmtLimitResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                editMchntFeeTxnAmtLimitResponse.setRespCode(((BizException) e).getCode());
                editMchntFeeTxnAmtLimitResponse.setRespDesc(e.getMessage());
            } else {
                editMchntFeeTxnAmtLimitResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                editMchntFeeTxnAmtLimitResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }
        return editMchntFeeTxnAmtLimitResponseResult;
    }

}
